export default {
  // method 请求方式
  // data   请求数据
  // url   请求路径
  // that 当前对象
  request(params, callBack) {
    var that = this;
    if (!params.method && params.status !== 1) {
      if (params.data) {
        params.url += "?" + that.convertObj(params.data);
        params.data = {};
      }
    }
    const loading = params.that.$loading({
      lock: true,
      // text: "加载中...",
      // spinner: "el-icon-loading",
      background: "rgba(255, 255, 255, 0)"
    });
    if (params.url === "auth/upload/file_image") {
      params.headers = { "content-type": "multipart/form-data" };
    }
    params.status = 1;
    params.that
      .$http({
        method: params.method || "GET",
        url: params.url,
        data: params.data || {},
        validateStatus: function(status) {
          return status >= 200 && status < 600;
        },
        headers: params.headers || {}
      })
      .then(function(response) {
        const res = response.data;
        loading.close();
        if (res.code === 6781) {
          that.message({ that: params.that, message: res.msg, type: "error" });
          return params.that.$router.push("/");
        } else if (
          res.code === 1001 ||
          res.code === 1002 ||
          res.code === 1004
        ) {
          localStorage.removeItem("token_type");
          localStorage.removeItem("token");
          return params.that.$router.push("/login");
        } else if (res.code === 1003) {
          return that.refresh_token(params, callBack);
        } else if (res.code === 4444 || res.code === 4222) {
          that.message({ that: params.that, message: res.msg, type: "error" });
        }
        callBack && callBack(res);
      });
  },
  // 提示信息
  message(params) {
    params.that.$message({
      message: params.message || "加载中...",
      type: params.type || "success",
      duration: params.duration || 2000,
      center: params.center !== false,
      offset: params.offset || 50
    });
  },
  // 刷新token
  refresh_token(params, callBack) {
    var that = this;
    that.request(
      {
        that: params.that,
        method: "PUT",
        url: "auth/index/refresh"
      },
      res => {
        if (res.code === 8888) {
          localStorage.setItem("token", res.data.token);
          localStorage.setItem("token_type", res.data.token_type);

          that.request(params, callBack);
        } else {
          localStorage.removeItem("token_type");
          localStorage.removeItem("token");
          params.that.$router.push("/login");
        }
      }
    );
  },
  // 将对象转换为url
  convertObj(data) {
    var _result = [];
    for (var key in data) {
      var value = data[key];
      if (value !== null) {
        if (value.constructor === Array) {
          if (value.length > 0) {
            _result.push(key + "=" + value.join("|"));
          }
        } else {
          _result.push(key + "=" + value);
        }
      }
    }
    return _result.join("&");
  },
  // 弹框
  confirm(params, callBack) {
    params.that
      .$confirm(params.content || "确定要删除吗?", params.title || "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
      .then(() => {
        callBack && callBack();
      });
  }
};
